package org.dtrd.modules.evaluation.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.modules.evaluation.entity.bean.SasInfo;
import org.dtrd.modules.evaluation.entity.dto.SasDTO;
import org.dtrd.modules.evaluation.entity.po.DtrdEntEvaluationRdSas;
import org.dtrd.modules.evaluation.mapper.DtrdEntEvaluationRdSasMapper;
import org.dtrd.modules.evaluation.service.IDtrdEntEvaluationRdSasService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.StringJoiner;

/**
 * <p>
 * 焦虑评估（SAS） 服务实现类
 * </p>
 *
 * @author QR
 * @since 2021-09-26
 */
@Service
@DS("multi-datasource1")
public class DtrdEntEvaluationRdSasServiceImpl extends ServiceImpl<DtrdEntEvaluationRdSasMapper, DtrdEntEvaluationRdSas> implements IDtrdEntEvaluationRdSasService {

    @Override
    public SasInfo saveSasInfo(SasDTO dto) {
        StringJoiner joiner = new StringJoiner(",");
        int scoreTotal = 0;
        int scoreStandard;
        int tmpScore;
        for (int i = 0; i < dto.getAnswers().length; i++) {
            tmpScore = dto.getAnswers()[i];
            joiner.add(tmpScore+"");
            if (5 == i + 1 || 9 == i + 1 || 13 == i + 1 || 17 == i + 1 || 19 == i + 1) {
                tmpScore = 5 - tmpScore;
            }
            scoreTotal += tmpScore;
        }
        scoreStandard = (int) (scoreTotal * 1.25);
        String output = "";
        if (scoreStandard < 50) {
            output = "您的得分属于正常范围，没有焦虑症状。";
        } else if (scoreStandard <= 59) {
            output = "您的得分高于50分，有轻度焦虑症状。";
        } else if (scoreStandard <= 69) {
            output = "您的得分高于60分，有中度焦虑症状。";
        } else {
            output = "您的得分高于70分，有重度焦虑症状。";
        }
        // 保存进数据库
        DtrdEntEvaluationRdSas sas = new DtrdEntEvaluationRdSas();
        sas.setInput(joiner.toString());
        sas.setCreateTime(new Date());
        sas.setScoreStandard(scoreStandard);
        sas.setScoreTotal(scoreTotal);
        sas.setOutput(output);
        if (dto.getPatientId() != null) {
            sas.setUserType(dto.getUserType());
            sas.setSeverity(0f);
            sas.setPatientId(dto.getPatientId());
            save(sas);
        }
        return new SasInfo().parseFromPo(sas);
    }

    @Override
    public SasInfo getSasInfoLatest(Integer patientId) {
        LambdaQueryWrapper<DtrdEntEvaluationRdSas> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(DtrdEntEvaluationRdSas::getPatientId, patientId)
                .eq(DtrdEntEvaluationRdSas::getIsDel, 0)
                .orderByDesc(DtrdEntEvaluationRdSas::getCreateTime)
                .last("limit 1");
        DtrdEntEvaluationRdSas sas = baseMapper.selectOne(lambdaQueryWrapper);
        SasInfo sasInfo = new SasInfo();
        if (sas != null) {
            sasInfo = sasInfo.parseFromPo(sas);
        }
        return sasInfo;
    }
}
